package com.hok.misc.ftp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

public class CommandExecutor {

	public static void main(String[] args) throws JSchException, IOException {
		String host = "10.10.12.11";
		String user = "billing";
		String pwd = "billing";
		String command = "ls -all";
		Properties config = new Properties(); 
		config.put("StrictHostKeyChecking", "no");
		
		String osName = System.getProperty("os.name");
		StringBuffer sb = new StringBuffer();
		JSch jsch = new JSch();
//		if (osName.toUpperCase().indexOf("WINDOWS") > -1) {
//			jsch.setKnownHosts("c:\\known_hosts");
//		} else {
//			jsch.setKnownHosts("/root/.ssh/known_hosts");
//		}
		Session session = jsch.getSession(user, host, 22);
		session.setPassword(pwd);
		session.setConfig(config);

		session.connect();
		Channel channel = session.openChannel("exec");
		((ChannelExec) channel).setCommand(command);
		InputStream in = channel.getInputStream();
		channel.connect();
		int nextChar;
		while (true) {
			while ((nextChar = in.read()) != -1) {
				sb.append((char) nextChar);
			}
			if (channel.isClosed()) {
				System.out.println("exit-status: " + channel.getExitStatus());
				break;
			}
			try {
				Thread.sleep(1000);
			} catch (Exception ee) {
			}
		}
		channel.disconnect();
		session.disconnect();
		System.out.println(sb.toString());
		// return sb.toString();
	}

	public static void c1() {
		try {
			String[] cmd = new String[] { "/bin/sh", "-c", " ls " };
			Process ps = Runtime.getRuntime().exec(cmd);

			BufferedReader br = new BufferedReader(new InputStreamReader(
					ps.getInputStream()));
			StringBuffer sb = new StringBuffer();
			String line;
			while ((line = br.readLine()) != null) {
				sb.append(line).append("\n");
			}
			String result = sb.toString();

			System.out.println(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static String execute(String host, String user, String pwd, String command) throws JSchException, IOException {
		Properties config = new Properties(); 
		config.put("StrictHostKeyChecking", "no");
		StringBuffer sb = new StringBuffer();
		JSch jsch = new JSch();
		Session session = jsch.getSession(user, host, 22);
		session.setPassword(pwd);
		session.setConfig(config);

		session.connect();
		Channel channel = session.openChannel("exec");
		((ChannelExec) channel).setCommand("cd temp\nll");
		InputStream in = channel.getInputStream();
		channel.connect();
		int nextChar;
		while (true) {
			while ((nextChar = in.read()) != -1) {
				sb.append((char) nextChar);
			}
			if (channel.isClosed()) {
				//System.out.println("exit-status: " + channel.getExitStatus());
				break;
			}
		}
		exec(session.openChannel("exec"));
		channel.disconnect();
		
		
		session.disconnect();
		System.err.println(sb.toString());
		return sb.toString();
	}
	
	private static void exec(Channel channel) throws IOException, JSchException {
		((ChannelExec) channel).setCommand("ll");
		InputStream in = channel.getInputStream();
		channel.connect();
		int nextChar;
		StringBuffer sb = new StringBuffer();
		while (true) {
			while ((nextChar = in.read()) != -1) {
				sb.append((char) nextChar);
			}
			if (channel.isClosed()) {
				//System.out.println("exit-status: " + channel.getExitStatus());
				break;
			}
		}
		channel.disconnect();
		System.out.println(sb.toString());
	}
}
